package create.singleton;

/**
 * @author dali
 * @date 2021-07-06 11:26
 * @Description 线程不安全 懒汉---只有在需要该对象的时候才创建
 */

public class LanHan1 {
    public static void main(String[] args) {
        LanHan1 instance1 = getInstance();
        LanHan1 instance2 = getInstance();
        System.out.println(instance1 == instance2);//true
    }
    //防外部new
    private LanHan1() {}
    //静态定义
    private static LanHan1 instance;
    //返回对象时获取
    public static LanHan1 getInstance() {
        //如果为空则创建
        if (instance == null) {
            instance = new LanHan1();
        }
        return instance;
    }
}
